$(function () {
	//动画中的点击和移动
	$("#canvas").click(function () {
		if(state == START || state == CHOOSE){
				state = STARTING;
		}
	});

	$("#canvas").mousemove(function (e) {
		if(state == PAUSE){
			state = RUNNING;
		}
		plainX = e.offsetX;
		plainY = e.offsetY;
	});

	$("#canvas").mouseout(function (e) {
			if(state == RUNNING){
				state = PAUSE;
			}	
	});

	//添加白框和英雄声音
	$("#choose>div").mouseover(function () {
		$(this).addClass("chooseHover");

		var id=$(this).attr("id");
		switch(id){
			case 'blue':
			changeMusic(false,"hero","audio/Jayce.mp3");
			break;
			case 'pink':
			changeMusic(false,"hero","audio/Tristana.mp3");
			break;
			case 'red':
			changeMusic(false,"hero","audio/Lux.mp3");
			break;
			case 'yellow':
			changeMusic(false,"hero","audio/Corki.mp3");
			break;
		}
	});
	//移除白框
	$("#choose>div").mouseout(function () {
		$(this).removeClass("chooseHover");
		$("#bgImage").css({"display":"none"});
	});
	//修改背景
	$("#choose>div").mouseover(function () {
		var u =" url(source/choose/"+$(this).attr('id')+".png)";
		$("#bgImage").css({"background":u});
		$("#bgImage").fadeIn("slow");
	});

	//选择飞机
	$("#choose>div").click(function () {
		var color = $(this).attr("id");
		//修改HeroLoading 和 Her索引
		switch(color){
			case "blue":
			HeroLoading.index = 0;
			Hero.index = 0;	
			pClass = 1;
			break;
			case "pink":
			HeroLoading.index = 4;
			Hero.index = 8;
			pClass = 2;	
			break;
			case "red":
			HeroLoading.index = 8;
			Hero.index = 15;
			pClass = 3;	
			break;
			case "yellow":
			HeroLoading.index = 12;
			Hero.index = 22;
			pClass = 4;	
			break;
		};	
		//隐藏begin
		$("#chooseFly").slideUp();
		//状态从CHOOSE变成START
		state++;

		//**开始改变对象起始状态和设置定时器（主体动画）
		loading = new LOADING(HeroLoading);
		hero = new HERO(Hero);
		Do = setInterval(animat,10);
	});

	//开始的事件
	//判断是否有名字
	checkName();

	//登陆操作
	$.formValidator.initConfig({
		formID:"nameForm",
		validatorGroup:"1"
	});
	//初始化 登录 相关的操作
	$("#ct").formValidator({
		validatorGroup:"1",
		onShow:"",
		onFocus:"只允许输入中文名",
		onCorrect:"可以注册"
	}).inputValidator({
		min:6,
		max:12,
		onError:"用户名3-6位"
	}).regexValidator({
		regExp:"[\u4E00-\u9FA5][^1]",
		onError:"只能为中文名"
	});

	//创建名字按钮
	$("#create").click(function () {
		if($.formValidator.pageIsValid("1")){
			//增加音效
			changeMusic(false,"bt","audio/point.mp3");

			name = $("#ct").val();
			var sentence = name+"，祝您愉快~"
			$("#name").html(sentence);
			localStorage.name = name;
			$("#beginBtn").removeAttr("disabled");
		}
	});
	//开始游戏
	$("#beginBtn").click(function () {
		//增加音效
		changeMusic(false,"bt","audio/point.mp3");
		$("#begin").hide();
	});
	//道具图鉴
	$("#tipBtn").click(function () {
		//增加音效
		changeMusic(false,"bt","audio/point.mp3");
		$("#tips").toggleClass("tips");
	});
	//重新开始游戏
	$("#leftBtn").click(function () {
		//增加音效
		changeMusic(false,"bt","audio/point.mp3");
		location.reload(); 
	});
	//查看排行榜
	$("#rightBtn").click(function () {
		//增加音效
		changeMusic(false,"bt","audio/point.mp3");
		//控制排行榜的出现和按钮的更改
		$("#rank").toggleClass("dis");
		var value = $("#rightBtn").val();
		if(value == "查看排行"){
			$("#rightBtn").val("关闭排行");
		}else{
			$("#rightBtn").val("查看排行");
		};
		//获取排行榜
		getRank();
	});

	//去除伪加载动画
	setTimeout("$('#loadImage').hide()",3000);
});



//检查是否是本地储存名字
function checkName() {
	if(localStorage.name){
		$("#name").empty();
		name = localStorage.name;
		var sentence = name+"，您好，欢迎回来~"
		$("#name").html(sentence);
	}else{
		$("#beginBtn").attr("disabled","true");
	}
}
//更新本地分数
function localScore() {
	$(".userName").html(name);
	$("#over dd:eq(0)").html(score);
	$("#over dd:eq(1)").html(function () {
		return (level*0.1+1)+"×"+score;
	});
	$("#over dd:eq(2)").html(function () {
		return life+"×"+"100";
	});
	$("#over dd:eq(3)").html(function () {
		return parseInt(score*(level*0.1+1)+life*100);
	});
	score = parseInt(score*(level*0.1+1)+life*100);
}
//提交分数
function subScore() {
	$.ajax({
		type:"POST",
		url:"http://121.42.205.141/fly/server/put.php",
		data:"name="+name+"&life="+life+"&score="+score+"&pClass="+pClass+"&level="+parseInt(level),
		success:function (msg) {
			//超过:修改成功，修改失败  
			//没超
			//没有记录:插入成功，插入失败
			switch(msg){
				case '修改成功':
				$("#over p:last").html("刷新自己的记录!成绩已经保存于云端");
				break;
				case '修改失败':
				$("#over p:last").html("刷新自己的记录!但是成绩没有保存成功");
				break;
				case '没超':
				$("#over p:last").html("没有超过自己的最佳记录哦~再接再厉");
				break;
				case '插入成功':
				$("#over p:last").html("创建自己的记录~成绩已经保存于云端");
				break;
				case '插入失败':
				$("#over p:last").html("创建自己的记录失败...请联系管理员");
				break;
			}
			
		}
	});
};
//查看排行榜
function getRank() {
	$.ajax({
		type:"GET",
		url:"http://121.42.205.141/fly/server/rank.php",
		success:function (msg){
			msg = eval(msg);
			for(var i=0;i<msg.length;i++){
				$(".name:eq("+i+")").html(msg[i].name);
				$(".rlife:eq("+i+")").html(msg[i].life);
				$(".rlevel:eq("+i+")").html(msg[i].level);
				$(".rscore:eq("+i+")").html(msg[i].score);
				$(".tdc:eq("+i+")").addClass(function () {
					return "p"+msg[i].pClass;
				});
			};
			//排行榜成功刷新音效
			changeMusic(true,'bg','audio/score.mp3');
		}
	});
};
//修改音乐函数: 判断是否在循环中，容器id，音乐链接
function changeMusic(boolean,id,src) {
	if(boolean){
		if($("#"+id).attr("src") == src){
			return false;
		}else{
			$("#"+id).removeAttr("src");
			$("#"+id).attr({"src":src});
		}
	}else{
		$("#"+id).removeAttr("src");
		$("#"+id).attr({"src":src});
	}
	
};

